/**  
 * All rights Reserved, Designed By www.woniuxy.com
 * @Title:  Account.java   
 * @Package com.woniu.domain   
 * @Description:    Description  
 * @author: 蜗牛学院  
 * @date:   2019年8月27日 下午7:37:48
 * @user:   Administrator  
 * @version V1.0 
 * @Copyright: 2019 www.woniuxy.com Inc. All rights reserved. 
 * 注意：本内容仅限于成都蜗牛创想科技有限公司，禁止外泄以及用于其他的商业目
 */ 
package com.woniu.ssm.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.woniu.ssm.domian.User;
import com.woniu.ssm.service.IUserService;

/**
 * @author Administrator
 *
 */
@Controller
@RequestMapping("user")
public class UserController {
	@Autowired
	IUserService userService ;
	
	
	@RequestMapping("/login")
	public String login(String uname,String upass,HttpServletRequest req){
		
		User user = userService.findUserByName(uname);
		
		if(user != null) {
			if(user.getUpass().equals(upass)) {
				req.getSession().setAttribute("user", user);
				
				//重定向 到查询用户列表
				String path = req.getContextPath();
				return "redirect:list.do";
			}else {
				req.setAttribute("errorinfo", "密码错误");
			}
		}else {
			req.setAttribute("errorinfo", "用户名不存在");
			
		}
		return "list";
	}
//	@RequestMapping("/list.do")
//	public ModelAndView list(){
//		List<User> users = userService.findList();
//		
//		ModelAndView mv = new ModelAndView();
//		mv.addObject("lists", users);
//		mv.setViewName("list");
//		return mv ;
//	}
	@RequestMapping("/list")
	public String list(Map map,Model model) {
		List<User> lists = userService.findList();
		//map.put("lists", lists);
		model.addAttribute("lists",lists);
		return "list";
	}
	@RequestMapping("/add")
	public String add(User user,MultipartFile imgUpload) throws IllegalStateException, IOException {
		//图片保存路径 
		String path = "D:\\img" ;
		//获得上传文件的名字 
		String fileName = imgUpload.getOriginalFilename();
		//当前时间毫秒数
		String currentTime = new Date().getTime()+"";
		//图片新的名字 
		fileName = currentTime + fileName.substring(fileName.lastIndexOf("."));
		
		user.setImgpath(fileName);
		
		//上传 
		imgUpload.transferTo(new File(path+File.separator+fileName));
		userService.add(user);
		return "redirect:list.do";
	}
	@RequestMapping("/selectuser")
	public String selectUser(Map map,User user) {
		User u = userService.findUserById(user.getUid());
		map.put("u", u);
		return "update";
	}
	@RequestMapping("/update")
	public String update(User user,MultipartFile imgUpload) throws IllegalStateException, IOException {
		//保存图片路劲
		String path = "D:\\img";
		//获得上传图片文件名字
		String fileName = imgUpload.getOriginalFilename();
		String currentTime = new Date().getTime()+"";
		fileName = currentTime+fileName.substring(fileName.lastIndexOf("."));
		user.setImgpath(fileName);
		//上传
		imgUpload.transferTo(new File(path+File.separator+fileName));
		userService.modify(user);
		return "redirect:list.do";
	}
	@RequestMapping("/del")
	public String del(int uid) {
		userService.remove(uid);
		return "redirect:list.do";
	}
	@RequestMapping("/showlist")
	public void showList(Map map,HttpServletRequest req,HttpServletResponse resp) throws IOException {
		List<User> lists = userService.findList();
		resp.setCharacterEncoding("utf-8");
		PrintWriter out = resp.getWriter();
		String json = JSON.toJSONString(lists);
		out.print(json);
		//return "index";
	}
}
